package library;

import java.io.Serializable;
import java.util.Observable;

public class Book extends Observable
implements Serializable, Comparable<Book>
{
	private static final long serialVersionUID = 770974696235511969L;
	
	private int id;
	private String isbn;
	private String title;
	private String author;
	private BookState state;
	
	public Book(int id, String isbn, String title, String author)
	{
		this.id = id;
		this.isbn = isbn;
		this.title = title;
		this.author = author;
		this.state = BookState.AVAILABLE;
	}
	
	public int getId()
	{
		return id;
	}

	@Override
	public String toString()
	{
		return ("Book [" + id + ", " + isbn + ", " + title + ", " + author + ", " + state + "]");
	}
	
	public Lending lendBook(Student s) throws LendingException
	{
		if (state.equals(BookState.BORROWED))
			throw new LendingException("Cannot lend a lent book");
		state = BookState.BORROWED;
		
		return new Lending(this, s);
	}
	
	public void returnBook()
	{
		state = BookState.AVAILABLE;
	}

	
	
	@Override
	public boolean equals(Object obj)
	{
		if (obj instanceof Book)
			return this.id == ((Book)obj).id;
		
		return false;
	}

	@Override
	public int hashCode()
	{
		return (isbn.hashCode() + id + author.hashCode() + title.hashCode());
	}

	public String getIsbn()
	{
		return isbn;
	}

	public String getTitle()
	{
		return title;
	}

	public String getAuthor()
	{
		return author;
	}

	public BookState getState()
	{
		return state;
	}

	@Override
	public int compareTo(Book other)
	{
		return this.id - other.id;
	}
	
	
}
